package org.nudt.common.security

import cn.dev33.satoken.exception.SaTokenException
import jakarta.servlet.http.HttpServletRequest
import org.nudt.common.model.ResponseData
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class GlobalExceptionHandler {
    
    @ResponseBody
    @ExceptionHandler(RuntimeException::class)
    fun handleException(
        e: RuntimeException,
        request: HttpServletRequest,
    ): ResponseData<String> {
        
        val resp = ResponseData.error(500, "is error", "")
        if (e is SaTokenException) {
            resp.code = 400
        }
        e.message?.let {
            resp.msg = it
        }
        return resp
    }
}